package org.rcy.ruicingmarket.cart;

import org.rcy.framework.cache.redis.CommonCache;
import org.rcy.ruicingmarket.cache.CartCacheGroup;
import org.rcy.ruicingmarket.common.utils.CollectionBeanUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
*
* @author RuicyWu
*@Date 2021-12-22 14:49:50
*/
@Service
public class CartService {
	@Autowired
    private CommonCache commonCache;



/**
 * 根据id删除Car
 * @param vo
 */

   public void remove(CartDelVo vo){
      Object data = commonCache.getData(CartCacheGroup.CART, vo.getUid());
      if(data != null) {
         List<ShoppingCart> carts = (List<ShoppingCart>)data;
         Set<String> set = vo.getSkuIds().stream().collect(Collectors.toSet());
         Iterator<ShoppingCart> iterator = carts.iterator();
         while (iterator.hasNext()){
            ShoppingCart next = iterator.next();
            if(set.contains(next.getSkuId())){
               iterator.remove();
            }
         }
         commonCache.cache(CartCacheGroup.CART,vo.getUid(),carts,60*60*24*15);
      }
   }

/**
 * 新增Car
 * @param vo
 */

   public void add(CartAddVo vo)
   {
      CartShowVo carShowVo = new CartShowVo();
      ShoppingCart cart = new ShoppingCart();
      BeanUtils.copyProperties(vo,cart);
      List<ShoppingCart> carts = new ArrayList<>();
      carts.add(cart);
      commonCache.cache(CartCacheGroup.CART,vo.getUid()+"",carts,60*60*24*15);
   }


    /**
     * 根据uid和skuId查询购物车中记录，看是否已有记录
     * @param uid
     * @param skuId
     * @return
     */
   public boolean findByUidAndBid(String uid,String skuId)
   {
      List<ShoppingCart> carts = (List<ShoppingCart>)commonCache.getData(CartCacheGroup.CART, uid);
      for (ShoppingCart cart : carts) {
         if(cart.getSkuId().equals(skuId)) {
            return true;
         }
      }
      return false;
   }

   /**
    * 修改数量
    * @param vo
    */

   public void editCount(CartEditVo vo){
      List<ShoppingCart> carts = (List<ShoppingCart>)commonCache.getData(CartCacheGroup.CART,vo.getUid()+"");
      for (ShoppingCart cart : carts) {
         if(cart.getSkuId().equals(vo.getSkuId())) {
            cart.setCount(cart.getCount() + vo.getCount());
            break;
         }
      }
      Iterator<ShoppingCart> iterator = carts.iterator();
      while (iterator.hasNext()){
         ShoppingCart next = iterator.next();
         if(next.getSkuId().equals(vo.getSkuId()+"") && next.getCount() <= 0){
            iterator.remove();
         }
      }
      commonCache.cache(CartCacheGroup.CART,vo.getUid()+"", carts,60*60*24*15);
   }

   /**
    * 查询用户购物车列表
    * @param uid
    * @return
    */
   public List<CartShowVo> getCarListByUid(Long uid){
      List<ShoppingCart> carts = (List<ShoppingCart>)commonCache.getData(CartCacheGroup.CART,uid + "");
      return CollectionBeanUtils.copyListProperties(carts, CartShowVo::new);
   }
}
